<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 16:35
 */
namespace app\admin\model;
use think\Model;
class OrderModel extends Model{

    /**
     * @param $order_id
     * @return mixed
     * 获取订单详情
     */
    public function getOrderInfo($order_id){
        $order_info = db('Order')
            ->alias('o')
            ->join('JOIN __REGION__ r','r.id = o.country or r.id = o.province or r.id = o.city or r.id = o.district','LEFT')
            ->join('JOIN __USER__ u ',' u.id = o.user_id','LEFT')
            ->field('o.*, group_concat(r.name) as region_address, u.nickname')
            ->where(['o.order_id'=>$order_id])
            ->find();
        //订单商品信息
        $order_info['goodsList'] = db('OrderGoods')->where(['order_id'=>$order_id])->select();
        //订单操作日志
        $order_info['action'] = db('OrderAction')
            ->alias('oa')
            ->join('JOIN __USER__ u ',' u.id = oa.action_user','LEFT')
            ->where(['oa.order_id'=>$order_id])
            ->field('oa.*, u.nickname')
            ->order('oa.log_time desc')
            ->select();
        return $order_info;
    }



    /**
     * @param $order
     * @return array
     * 获取订单当前可操作的按钮
     */
    public function getOrderButton($order){
        /*
         *  操作按钮汇总 ：付款、设为未付款、确认、取消确认、无效、去发货、确认收货、申请退货
         *
         */
        $os = $order['order_status'];//订单状态
        $ps = $order['pay_status'];//支付状态
        $btn = array();
        if($ps == 0 && $os == 0){
//                $btn['pay'] = '付款';
        }elseif($os == 0 && $ps == 1){
//        		$btn['pay_cancel'] = '设为未付款';
            $btn['confirm'] = '开始工作';
        }elseif($os == 2 && $ps == 1){
//        		$btn['cancel'] = '取消确认';
            $btn['delivery'] = '完成';
        }

        if($os == 1 && $ps == 1){
//            $btn['delivery_confirm'] = '确认收货';
//            $btn['refund'] = '申请退货';
        }elseif($os == 2 || $os == 4){
//            $btn['refund'] = '申请退货';
        }elseif($os == 3 || $os == 5){
//        	$btn['remove'] = '移除';
        }
        if($os != 5){
//        	$btn['invalid'] = '无效';
        }
        return $btn;
    }

    public function orderProcessHandle($order_id,$act){
        $updata = array();
        switch ($act){
            case 'pay': //付款
//                update_pay_status($order_id); // 调用付款方法
                return true;
//            case 'pay_cancel': //取消付款
//                $updata['pay_status'] = 0;
//                $this->order_pay_cancel($order_id);
//                return true;
            case 'confirm': //开始工作
                $updata['order_status'] = 2;
                $updata['shipping_time'] = time();
                break;
            case 'cancel': //取消确认
                $updata['order_status'] = 0;
                break;
            case 'invalid': //作废订单
                $updata['order_status'] = 5;
                break;
            case 'remove': //移除订单
                $this->delOrder($order_id);
                break;
            case 'delivery'://确认收货
                $updata['order_status'] = 4; // 已收货
                $updata['confirm_time'] = time(); // 收货确认时间
                break;
            default:
                return true;
        }
        return db('order')->where("order_id=$order_id")->isUpdate(true)->save($updata);//改变订单状态
    }


    /*
     * 订单操作记录
     */
    public function orderActionLog($order_id,$action,$note=''){
        $order = db('order')->where(array('order_id'=>$order_id))->find();
        $data['order_id'] = $order_id;
        $data['action_user'] = 0;
        $data['action_note'] = $note;
        $data['order_status'] = $order['order_status'];
        $data['pay_status'] = $order['pay_status'];
        $data['shipping_status'] = $order['shipping_status'];
        $data['log_time'] = time();
        $data['status_desc'] = $action;
        return db('order_action')->save($data);//订单操作记录
    }

}